#ifndef VIEWOBJECT_H
#define VIEWOBJECT_H
#include <QObject>
#include <QWidget>
#include "paramobject.h"
#include <QMovie>
#include <errno.h>
#include <QMouseEvent>
#include <QApplication>
#include <memory>
#include <QMetaType>
#include <QRect>
#include <QMenu>
#include <QAction>
#include <QFileInfo>
#include <QLabel>
#include "huaqimovie.h"
#include "hqlabel.h"
#include "common.h"

class ViewObject : public QWidget
{
    Q_OBJECT
public:
    explicit ViewObject(QWidget *parent = 0)
    {
        Q_UNUSED(parent);
        m_objectParam = NULL;
        mSelect = false;
        mProtocolData = NULL;
        m_hq_label = NULL;
        url = "";
        old_url = "";
        url_with_suffix = "";
        mFilename = "";
        m_change = false;
        m_acrossxml_obj_has_shown = false;
        mOffsetX   = 0;
        m_change_x = 0;
        m_change_y = 0;
        m_change_w = 0;
        m_change_h = 0;

        m_layout = new QHBoxLayout;
        m_layout->setContentsMargins(0, 0, 0, 0);
        setContentsMargins(0, 0, 0, 0);
        setLayout(m_layout);
    }

    virtual ~ViewObject(){}

    virtual void ShowObject() = 0;//控件显示接口

    virtual void GetAttribute(std::string&) = 0;//获取属性值

    virtual void SetAttribute(std::string) = 0;//设置属性值

    virtual void ParseXML(QDomElement* ) = 0;//解析xml值

    virtual void PacketXML(QDomElement*) = 0;//生成xml值

    inline void SetProtocolData(ProtocolData *data) {mProtocolData = data;}

    inline void SetShowWidget(QWidget *parent) {mParent = parent;}

    inline void SetResPathPrefix(QString prefix) {mPrefix = prefix;}

    inline void SetFilename(QString filename) {mFilename = filename;}

    inline void SetPrefix(QString prefix) {mPrefix = prefix;}

    inline ParamObject* GetObjectParam() {return m_objectParam;}

    inline QString GetPrefix() {return mPrefix;}

    inline bool GetSelect() {return mSelect;}

    inline QString GetFilename() {return mFilename;}

    void SetObjectParam(ParamObject &param);

    //获取最终显示的url
    QString GetFinalUrl();

    void Update(std::string json);

    void set_style();

    void Select();

    void Cancel();

    void Up();

    void Down();

    void Right();

    void Left();

    void location(int x, int y, int w, int h);

    void AddSuffix(QString &path);

    void StartHqv();

    virtual QString FindHqt(QString prefix, QString url);

    virtual QString FindCss(QString prefix, QString url_origin, QString url_with_suffix);

    virtual QString FindFile(QString prefix, QString name);

    virtual void FinalShow(QString url);

    virtual void RunItemsChange(QMap<QString, change_property> items_change);

    //只有小火车重新实现
    virtual void ShowUrl();

    //以下三个函数小火车控件需要重写
    virtual void ReLoadRes();

    virtual void AddSuffixAndReloadPic(QString suffix);

    virtual void ReRunItemsChangeAndShow(QMap<QString, change_property> items_change);

public:

    HqLabel*        m_hq_label;
    QHBoxLayout*    m_layout;
    ParamObject*    m_objectParam;
    QPoint          m_point;
    QPoint          mOffset;
    bool            mSelect;
    bool            m_acrossxml_obj_has_shown;
    QString         mPrefix;
    QString         mFilename;
    ProtocolData*   mProtocolData;
    QWidget*        mParent;
    int             mOffsetX;
    QString         url;
    QString         old_url;
    QString         url_with_suffix;

    bool m_change;
    int  m_change_x;
    int  m_change_y;
    int  m_change_w;
    int  m_change_h;

public slots:
    void slt_rightButton_copy();
    void slt_rightButton_edit();
    void slt_rightButton_delete();
    void slt_BeginShow();

signals:
    void mouseRelease(QPoint);
    void sgl_viewobject_select_to_editwidget(QString);
    void sgl_viewobject_update_to_editwidget(QString);
    void signalDelete(QString);
    void signalCopy(QString);
    void signalSelect(QString);
    void signalEdit(QString);
    void signalSelectAll();

protected:
#ifndef __linux__
    void mouseMoveEvent(QMouseEvent *event);
    void mousePressEvent(QMouseEvent *event);
    void keyPressEvent(QKeyEvent *event);
#endif
};

typedef struct ViewObjectInfo
{
    ViewObject*     object;
    std::string     attribute;
    QString unique_name;
    ViewObjectInfo()
    {
        object = NULL;
        attribute = "";
        unique_name = "";
    }
}ViewObjectInfo;

#endif
